﻿using AutoMapper;
using Entity.Finance.DTO.DailyReimburse;
using Entity.Finance.Entity;
using Entity.Finance.View.DailyReimburse;
using FrameWork.DataContext;
using FrameWork.DataHelper;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;
using static Common.Enum.StateEnum;

namespace Service.Finance
{
    public class DailyReimburseService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 自动映射
         */
        private readonly IMapper _mapper;

        /**
         * 构造注入
         */
        public DailyReimburseService(DataContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        /**
         * 获取日常报销单列表
         */
        public async Task<RequestResponseModel<PageResult<DailyReimburseView>>> GetDailyReimburseList(DailyReimburseSearchDTO dailyReimburseSearchDTO)
        {
            //筛选
            var query = _context.DailyReimburseView
                .WhereIF(a => a.Code.Contains(dailyReimburseSearchDTO.Code), !string.IsNullOrEmpty(dailyReimburseSearchDTO.Code))
                .WhereIF(a => a.StaffCode.Contains(dailyReimburseSearchDTO.Code) || a.StaffName.Contains(dailyReimburseSearchDTO.Staff), !string.IsNullOrEmpty(dailyReimburseSearchDTO.Staff))
                .WhereIF(a => a.Status == dailyReimburseSearchDTO.Status, dailyReimburseSearchDTO.Status != null)
                ;

            //返回列表
            var dailyReimburseList = await query
                .OrderBy(a => a.CreateTime)
                .Page(dailyReimburseSearchDTO.CurrentPage, dailyReimburseSearchDTO.PageSize)
                .ToListAsync();

            //总数据量
            var pageCount = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<DailyReimburseView>(dailyReimburseList, pageCount);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        /**
         * 新增日常报销单
         */
        public async Task<RequestResponseModel<string>> AddDailyReimburse(DailyReimburseAddDTO dailyReimburseAddDTO)
        {
            //新增数据
            var dailyReimburse = _mapper.Map<DailyReimburseModel>(dailyReimburseAddDTO);
            dailyReimburse.Status = Common.Enum.StateEnum.ExamStatus.待审核;
            dailyReimburse.CreateTime = DateTime.Now;

            //生成报销审批单
            var examinReimburse = _mapper.Map<ExamineReimburseModel>(dailyReimburse);

            //给两张单据赋予id
            dailyReimburse.Id = Guid.NewGuid().ToString();
            examinReimburse.Id = Guid.NewGuid().ToString();
            examinReimburse.ReimburseId = dailyReimburse.Id;

            //更新表
            _context.DailyReimburse.Add(dailyReimburse);
            _context.ExamineReimburse.Add(examinReimburse);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "新增成功", dailyReimburse.Id);
        }

        /**
         * 编辑日常报销单
         */
        public async Task<RequestResponseModel<string>> PutDailyReimburse(DailyReimburseUpdateDTO dailyReimburseUpdateDTO)
        {
            //校验数据
            var query = await Valid.ValidId<DailyReimburseModel>(_context.DailyReimburse, dailyReimburseUpdateDTO.Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", dailyReimburseUpdateDTO.Id);
            };

            //搜索报销审批单
            var examinReimburse = await _context.ExamineReimburse.FirstOrDefaultAsync(a => a.ReimburseId == dailyReimburseUpdateDTO.Id);

            //生成新数据
            var dailyReimburse = _mapper.Map(dailyReimburseUpdateDTO, query);
            examinReimburse = _mapper.Map(dailyReimburseUpdateDTO, examinReimburse);

            //更新单据
            _context.DailyReimburse.Update(dailyReimburse);
            _context.ExamineReimburse.Update(examinReimburse);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "更新成功", dailyReimburseUpdateDTO.Id);
        }

        /**
         * 删除日常报销单
         */
        public async Task<RequestResponseModel<string>> DeleteDailyReimburse(string Id)
        {
            //是否有匹配资源
            var query = await Valid.ValidId<DailyReimburseModel>(_context.DailyReimburse, Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            }

            //搜索报销审批单
            var examineReimburse = await _context.ExamineReimburse.FirstOrDefaultAsync(a => a.ReimburseId == Id);

            //更新表
            _context.ExamineReimburse.Remove(examineReimburse);
            _context.DailyReimburse.Remove(query);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "删除成功", Id);
        }

        /**
         * 审核日常报销单
         */
        public async Task<RequestResponseModel<string>> Review(DailyReimburseReviewDto model)
        {
            //是否有匹配资源
            var dailyReimburse = await Valid.ValidId<DailyReimburseModel>(_context.DailyReimburse, model.Id);
            if (dailyReimburse == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", model.Id);
            }

            //搜索报销审批单
            var examineReimburse = await _context.ExamineReimburse.FirstOrDefaultAsync(a => a.ReimburseId == model.Id);
            if (examineReimburse == null)
            {
                return RequestResponse.CreateResponse(500, "未找到审核单", model.Id);
            }

            if (model.IsPass)
            {
                dailyReimburse.Status = ExamStatus.审核通过;
                examineReimburse.Status = ExamStatus.审核不通过;
            }
            else
            {
                dailyReimburse.Status = ExamStatus.审核通过;
                examineReimburse.Status = ExamStatus.审核不通过;

                dailyReimburse.UnPassRemark = model.UnPassRemark;
                examineReimburse.UnPassRemark = model.UnPassRemark;
            }
            examineReimburse.ExamineTime = DateTime.Now;

            //更新单据
            _context.DailyReimburse.Update(dailyReimburse);
            _context.ExamineReimburse.Update(examineReimburse);

            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "删除成功", model.Id);
        }


    }
}
